/**
 * Created by Elsa on 2017/8/8.
 */
layui.define(['form', 'layer','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer;

    function CurentTime() {
        var now = new Date();

        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日

        var hh = now.getHours();            //时
        var mm = now.getMinutes();          //分
        var ss = now.getSeconds();          //秒

        var clock = year + "-";

        if(month < 10)
            clock += "0";

        clock += month + "-";

        if(day < 10)
            clock += "0";

        clock += day + " ";

        if(hh < 10)
            clock += "0";

        clock += hh + ":";
        if (mm < 10) clock += '0';
        clock += mm + ":";

        if (ss < 10) clock += '0';
        clock += ss;
        return(clock);
    }

    //监听提交

    form.on('submit(casegly)', function(data) {
        $(this).attr('disabled','disabled');
        var flag = 0;
        var temp = data.field;
        if(!data.field.sex){
            data.field.sex = null;
        }
        for(var key in temp){
            var ss = "#"+key+"Info";
            if(key == "sex") {
                if(temp[key]){
                    $(ss).css("color", "green");
                    $(ss).attr("class", "fa fa-check");
                    flag++;
                }else{
                    $(ss).css("color","red");
                    $(ss).attr("class","fa fa-times");
                    if(data.field.name){
                        $("#all-info").html("*请选择性别");
                    } else {
                        $("#all-info").html("*请填写用户信息");
                    }

                }
            }
            if(!temp[key] && key != "sex"){
                $(ss).css("color","red");
                $(ss).attr("class","fa fa-times");
                $("#all-info").html("*请填写用户信息");
            }else {
                if(key == "name") {
                    var nameRegex=/(^[\u4e00-\u9fa5]{2,4}$)|(^[a-zA-Z]{3,7}$)/;
                    if(nameRegex.test(temp[key])){
                        $(ss).css("color", "green");
                        $(ss).attr("class", "fa fa-check");
                        flag++;
                    }else{
                        $(ss).css("color","red");
                        $(ss).attr("class","fa fa-times");
                        if(temp[key] == ""){
                            $("#all-info").html("*姓名不能为空");
                        }else {
                            $("#all-info").html("*姓名由2-4个中文字或3-7个英文字母组成");
                        }
                    }
                }
                if(key == "username") {
                    var nameRegex=/^1[345789]\d{9}$/;
                    if(nameRegex.test(temp[key])){
                        $(ss).css("color", "green");
                        $(ss).attr("class", "fa fa-check");
                        flag++;
                    }else{
                        $(ss).css("color","red");
                        $(ss).attr("class","fa fa-times");
                        if(temp[key] == ""){
                            $("#all-info").html("*手机号码不能为空");
                        }else {
                            $("#all-info").html("*请正确填写手机号码");
                        }
                    }
                }
                if(key == "userid") {
                    var nameRegex=/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|30|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|X)$/;
                    if(nameRegex.test(temp[key])){
                        $(ss).css("color", "green");
                        $(ss).attr("class", "fa fa-check");
                        flag++;
                    }else{
                        $(ss).css("color","red");
                        $(ss).attr("class","fa fa-times");
                        if(temp[key] == ""){
                            $("#all-info").html("*身份证号不能为空");
                        }else {
                            $("#all-info").html("*请正确填写身份证号");
                        }
                    }
                }
            }
        }
        if(flag==4){
            var fzruser = new Object();
            fzruser.name = data.field.name;
            fzruser.sex = data.field.sex;
            fzruser.username = data.field.username;
            fzruser.userid = data.field.userid;

            $.ajax({
                type:"POST",
                url:contextPath+"/user/fzr/new",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(fzruser),
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "负责人用户创建成功!"){
                        layer.closeAll();
                        layer.msg('负责人用户创建成功!<br>页面将在1秒后刷新...');
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        $('#suresubmit').attr('disabled',false);
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                        $("#all-info").html("*"+data.message);
                    }
                },
                error:function (e) {
                }
            });
            return false;
        } else{
            $('#suresubmit').attr('disabled',false);
            return false;
        }
    });

    exports('NewHead', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});